package com.comdev.biz;

import com.comdev.common.WebPath;
import com.comdev.orm.jdbc.DbKit;
import com.comdev.ut.crpt.Base64;
import com.comdev.ut.string.StringUT;
import com.comdev.vos.t_file;
import com.dbutils.QueryHelper;
import org.springframework.stereotype.Controller;

import java.io.File;

/**
 * User: zhu
 * Date: 13-8-31
 * Time: 下午12:51
 */
@Controller
public class FileBiz
{

    public void del(String objid)
    {
        String savepath = DbKit.getString(" SELECT  savepath_ from t_file  where objid='" + objid + "'");
        if (!StringUT.isEmpty(savepath))
        {
            if (new File(WebPath.getUploadRootPath() + savepath).exists())
            {
                new File(WebPath.getUploadRootPath() + savepath).delete();
            }

            QueryHelper.execute("delete from t_file  where objid='" + objid + "'");
        }


    }

    /**
     * 获取文件夹路径对应的base64编码
     *
     * @param url
     * @return
     */
    public static String getmovieurl(String url)
    {
        String out = Base64.encode_url(url, "UTF-8");
        return out;
    }


    /**
     * 根据编码后的code，获取实际的文件地址
     *
     * @param code
     * @return
     */
    public static String getmovieurl2(String code)
    {
        code = Base64.decode_url(code, "UTF-8");
        return code;
    }


    public static String get_moviename(String code)
    {
        return code.substring(code.lastIndexOf(File.separatorChar) + 1, code.length());
    }


    public static void main(String[] args)
    {
        String s1 = FileBiz.getmovieurl("F:\\Mp3\\白小平上坟（又名37号坟）\\0006.优酷网-郭德纲单口相声：《白小平上坟》（6）-0003.flv");
        System.out.println(s1);
        // FileBiz.getmovieurl2(code);
        String s2 = FileBiz.getmovieurl2(s1);
        System.out.println(s2);
    }


    /**
     * id存在，并且在文件夹中存在
     *
     * @param imgid
     * @return
     */
    public static boolean exist(String imgid)
    {
        int size = QueryHelper.query_Count("select objid from t_file where objid='" + imgid + "'");
        if (size == 0)
        {
            return false;
        }
        t_file   file= QueryHelper.query_Bean(t_file.class,"select * from t_file where objid='"+imgid+"'");

        String filepath = WebPath.getUploadRootPath() + file.getSavepath_();
        if (!new File(filepath).exists())
        {
            return false;
        }
        return true;
    }


}
